<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Http\Model\Type;
use Illuminate\Support\Facades\DB;


class TeibieController extends Controller{
    public function ShowType(){
        $result = Type::paginate(5);
        return view('admin/leibie/leibie',compact('result'));
    }
    // 删除
    public function TypeDel($id){
        // Type::find($typeid)->delete();
       
        Type::where('typeid',$id)->delete();
        return ['status'=>0,'msg'=>'删除成功'];
        // if($result>0){
        //     return back();
        // }
    } 
    // 新增
    public function TypeAdd(){
        $data['typename']=$_POST['mc']; 
        $data['typedes']=$_POST['ms'];
        $result = Type::insert($data);
        if($result>0){
            return redirect('admin/leibie');
        }
        else
        {
            return back();
        }
    }
      // 显示修改类别信息
      public function TypeExit($id){
        $result = Type::where('typeid',$id)->first();
        return view('admin/leibie/xgleibie',compact('result'));
    }

      // 修改类别
      public function TypeUp(){
        $id = $_POST['id'];
        $data['typename']=$_POST['mc'];
        $data['typedes']=$_POST['ms'];
        $result = Type::where('typeid',$id)->update($data);
        if($result>0){
            return redirect('admin/leibie');
        }
        else
        {
            return back();
        }
    }

     // 复选框删除
     public function TypeDelall(){
        foreach ($_POST['checked'] as $key => $id) {
          $result = Type::where('typeid',$id)->delete();
        }
        if($result>0){
            return redirect('admin/leibie');
        }
        else
        {
            return back();
        }
       
    }

}